{% extends 'index.html' %}
{% block page-content %}
<div id="page-wrapper">
    <div class="row">
         <div class="col-lg-12">
              <h1 class="page-header"><i class="fa  fa-print "></i>Ansible操作日志</h1>
         </div>
                <!-- /.col-lg-12 -->
    </div>
	<div class="row">
                <div class="col-lg-12">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                           	<i class="fa   fa-gear"></i> 操作日志
                        </div>
                        <div class="panel-body">

				                            
				           <ul class="nav nav-tabs">
				              <li class="active"><a href="#model" data-toggle="tab"><i class="fa fa-cubes"></i> Ansible模块</a>
				              </li>					           
				              <li><a href="#profile" data-toggle="tab"><i class="fa fa-tasks"></i> 剧本日志</a>
				              </li>
			              
				           </ul>
				
				                            
				           <div class="tab-content">
				              <div class="tab-pane fade in active" id="model">
				                   <div class="row">
				                   		<div class="col-lg-12">
				                   			<br>
				                   			<div class='col-sm-8'> 
				                   			</div>
										    <div class='col-sm-2'>  
										        <div class="form-group">   
										            <div class='input-group'>  
										                <input  type="text"  id="modelLogStartTime"  class="form-control form_datetime" data-date-format="yyyy-mm-dd hh:mm" placeholder="开始时间"> 
										                <span class="input-group-addon">  
										                    <span class="glyphicon glyphicon-calendar"></span>  
										                </span> 										                
										            </div>  
										        </div> 	
										    </div>
										    <div class='col-sm-2'>									    
										        <div class="form-group">    
										            <div class='input-group'>  
										                <input  type="text" id="modelLogEndTime"   class="form-control form_datetime" data-date-format="yyyy-mm-dd hh:mm"  placeholder="结束时间"> 									                
										                <span class="input-group-addon" onclick="searchModelLog()">  
										                    <span class="glyphicon glyphicon-search"></span>  										                    
										                </span> 																							                
										            </div>  
										        </div>  
										    </div> 											                   			
				                   		</div>
				                   		<div class="col-lg-12">
				                   			<br>
				                            <table width="100%" class="table table-striped table-bordered table-hover " id="modelTableList">
				                                <thead>
				                                    <tr>
				                                    	<th class="text-center">#</th>
				                                    	<th class="text-center">操作用户</th>
				                                        <th class="text-center">模块名称</th>
				                                        <th class="text-center">模块参数</th>
				                                        <th class="text-center">操作时间</th>
				                                        {% if perms.can_delete_log_ansible_model %}
				                                        	<th class="text-center">操作</th>
				                                        {% endif %}					                                        
				                                    </tr>
				                                </thead>
				                                <tbody>
				                                	{% for ds in modelList %}
				                                		<tr  id="modelLogs-{{ds.id}}">
				                                			<td class="text-center">
				                                				 {{ forloop.counter }}
				                                			</td>
				                                			<td class="text-center">{{ds.ans_user}} </td>
				                                			<td class="text-center">{{ds.ans_model}} </td>
				                                			<td class="text-center">{{ds.ans_args}} </td>
				                                			<td class="text-center">{{ds.create_time|date:"Y/m/d H:i  "}} </td>				                                			
					                                		<td class="text-center">
																<a href="javascript:" onclick="getAnsibleResult(this,'model',{{ds.id}})">
							                                		<button class="btn btn-primary btn-xs"  data-toggle="modal" data-target="#myAnsibleModelModal">					                                					
																		<i class="glyphicon glyphicon-zoom-in  bigger-110 icon-only"></i>
																	</button>
																</a>					                                		
					                                			{% if perms.can_delete_log_ansible_model %}
						                                			<a href="javascript:" onclick="deleteAnsibleModelLogs(this,{{ds.id}})">
							                                			<button class="btn btn-danger btn-xs">					                                					
																			<i class="glyphicon glyphicon-trash  bigger-110 icon-only"></i>
																		</button>
																	</a>
																{% endif %}	
															</td>
																			                                			
				                                		</tr>
				                                	{% endfor %}
				                                </tbody>
				                            </table>			                   			
										</div>
				                   </div>
				              </div>				           
				              <div class="tab-pane fade" id="profile">
				                   <div class="row">
				                   		<div class="col-lg-12">
				                   			<br>
				                   			<div class='col-sm-8'> 
				                   			</div>
										    <div class='col-sm-2'>  
										        <div class="form-group">   
										            <div class='input-group'>  
										                <input  type="text"  id="playbookLogStartTime"  class="form-control form_datetime" data-date-format="yyyy-mm-dd hh:mm" placeholder="开始时间"> 
										                <span class="input-group-addon">  
										                    <span class="glyphicon glyphicon-calendar"></span>  
										                </span> 										                
										            </div>  
										        </div> 	
										    </div>
										    <div class='col-sm-2'>									    
										        <div class="form-group">    
										            <div class='input-group'>  
										                <input  type="text" id="playbookLogEndTime"   class="form-control form_datetime" data-date-format="yyyy-mm-dd hh:mm"  placeholder="结束时间"> 									                
										                <span class="input-group-addon" onclick="searchPlayBookLog()">  
										                    <span class="glyphicon glyphicon-search"></span>  										                    
										                </span> 																							                
										            </div>  
										        </div>  
										    </div> 											                   			
				                   		</div>				                   
				                   		<div class="col-lg-12">
				                   			<br>
				                            <table width="100%" class="table table-striped table-bordered table-hover" id="playbookTableList">
				                                <thead>
				                                    <tr>
				                                    	<th class="text-center">任务id</th>
				                                    	<th class="text-center">操作用户</th>
				                                        <th class="text-center">剧本名称</th>
				                                        <th class="text-center">剧本内容</th>
				                                        <th class="text-center">操作时间</th>
				                                        {% if perms.can_delete_log_ansible_playbook %}
				                                        	<th class="text-center">操作</th>
				                                        {% endif %}					                                        
				                                    </tr>
				                                </thead>
				                                <tbody>
				                                	{% for ds in playbookList %}
				                                		<tr class="odd gradeX" id="playbookLogs-{{ds.id}}">
				                                			<td class="text-center">
				                                				 {{ds.id}}
				                                			</td>
				                                			<td class="text-center">{{ds.ans_user}} </td>
				                                			<td class="text-center">
				                                				<a href="/apps/playbook/modf/{{ds.ans_id}}/">
				                                					{{ds.ans_name}}
				                                				</a>
				                                			</td>
				                                			<td class="text-center">{{ds.ans_content}} </td>
				                                			<td class="text-center">{{ds.create_time|date:"Y/m/d H:i  "}}
				                                			</td>
				                                			<td class="text-center">
																<a href="javascript:" onclick="getAnsibleResult(this,'playbook',{{ds.id}})">
							                                		<button class="btn btn-primary btn-xs"  data-toggle="modal" data-target="#myAnsibleModelModal">					                                					
																		<i class="glyphicon glyphicon-zoom-in  bigger-110 icon-only"></i>
																	</button>
																</a>				                                							                                			
					                                			{% if perms.can_delete_log_ansible_playbook %}
					                                				<a href="javascript:" onclick="deleteAnsiblePlaybookLogs(this,{{ds.id}})">
						                                				<button class="btn btn-danger btn-xs">					                                					
																			<i class="glyphicon glyphicon-trash  bigger-110 icon-only"></i>
																		</button>
																	</a>
																{% endif %}	
															</td>				                                			
				                                		</tr>
				                                	{% endfor %}
				                                </tbody>
				                            </table>			                   			
										</div>
				                   </div>
				              </div>
				              
				           </div>


                            <!-- /.row (nested) -->
                        </div>
                        <!-- /.panel-body -->
                    </div>
                    <!-- /.panel -->
                </div>
                <!-- /.col-lg-12 -->
            </div>   
            
            
            <!-- 模态框（Modal） -->
			<div class="modal fade" id="myAnsibleModelModal" tabindex="-1" role="dialog" aria-labelledby="myAnsibleModelModalLabel" aria-hidden="true">
				<div class="modal-dialog" style="width:1280px;height:auto;">
					<div class="modal-content">
						<div class="modal-header">
							<button type="button" class="close" data-dismiss="modal" 
									aria-hidden="true">×
							</button>
							<h4 class="modal-title" id="myAnsibleModelModalLabel">
								执行结果
							</h4>
						</div>
						<div class="modal-body">
							<pre>
								<div id="ansible_task_result">
								</div>
							</pre>
						</div>
						<div class="modal-footer">
							<button type="button" class="btn btn-default" 
									data-dismiss="modal">关闭
							</button>
						</div>
					</div><!-- /.modal-content -->
				</div><!-- /.modal-dialog -->
			</div><!-- /.modal --> 
</div>


<script type="text/javascript">

		$(".form_datetime").datetimepicker({format: 'yyyy-mm-dd hh:mm'});

		function searchModelLog() {
			startTime = document.getElementById("modelLogStartTime").value;
			endTime = document.getElementById("modelLogEndTime").value;
			if( startTime.length==0 || endTime.length==0 ){
				window.wxc.xcConfirm("请注意选择时间", window.wxc.xcConfirm.typeEnum.error);
				return false;
			};
			$.ajax({
				dataType: "JSON",
				url:'/api/logs/search/model/',
				type:"POST",  //提交类似
				data:{
					"startTime":startTime,
					"endTime":endTime
				},
 				success:function(response){
					if (response['data'].length > 0){	
				        var tableBodys = '';
						for(var i=0;i<response['data'].length;i++){
            				var delHtml = '<a href="javascript:" onclick="deleteAnsibleModelLogs(this,'+ response['data'][i].id + ')">' +	
				            				'<button class="btn btn-danger btn-xs">' +						                                					
												'<i class="glyphicon glyphicon-trash  bigger-110 icon-only"></i>' +	
											'</button>' +	
										  '</a>'							
	                    	var tableBody = '<tr class="odd gradeX" id="playbookLogs-' + response['data'][i].id + '">' +
				                			'<td class="text-center">' + response['data'][i].id + '</td>' + 
				                			'<td class="text-center">' + response['data'][i].ans_user + '</td>' +
				                			'<td class="text-center">' + response['data'][i].ans_model + '</td>' +
				                			'<td class="text-center">' + response['data'][i].ans_args + '</td>' +
/* 				                			'<td class="text-center">' + response['data'][i].ans_server + '</td>' + */
				                			'<td class="text-center">' + response['data'][i].create_time.replace("T",' ').replace(/-/g,'/').slice(0,16) + '</td>' +				                                			
				                    		'<td class="text-center">' +
												'<a href="javascript:" onclick="getAnsibleResult(this,\'model\','+ response['data'][i].id + ')">' + 
				                            		'<button class="btn btn-primary btn-xs"  data-toggle="modal" data-target="#myAnsibleModelModal">' +					                                					
														'<i class="glyphicon glyphicon-zoom-in  bigger-110 icon-only"></i>' +
													'</button>' +
												'</a>';	
												if (response['perm'] == 1){
													var tableBody = tableBody + delHtml
												}
												else{
													var tableBody =  tableBody + '</td></tr>';
												}
	
							var tableBodys = tableBody + tableBodys;
						};	
					    var  tableHmtl = '<table width="100%" class="table table-striped table-bordered table-hover" id="modelTableList">' +
				        '<thead>' + 
				            '<tr>' +
				            	'<th class="text-center">#</th>' + 
				            	'<th class="text-center">操作用户</th>' +
				                '<th class="text-center">模块名称</th>' +
				                '<th class="text-center">模块参数</th>' +
/* 				                '<th class="text-center">服务器</th>' + */
				                '<th class="text-center">操作时间</th>' +
				                '<th class="text-center">操作</th>' +				                                        
				            '</tr>' +
				        '</thead>' +
				        '<tbody>' + tableBodys + '</tbody></table>';
					    document.getElementById("modelTableList").innerHTML = tableHmtl;		
					}
					else{
						window.wxc.xcConfirm("未查询到相关数据", window.wxc.xcConfirm.typeEnum.error);
					}
				}	 			
			})					
		}		
		
		function searchPlayBookLog() {
			startTime = document.getElementById("playbookLogStartTime").value;
			endTime = document.getElementById("playbookLogEndTime").value;
			if( startTime.length==0 || endTime.length==0 ){
				window.wxc.xcConfirm("请注意选择时间", window.wxc.xcConfirm.typeEnum.error);
				return false;
			};
			$.ajax({
				dataType: "JSON",
				url:'/api/logs/search/playbook/',
				type:"POST",  //提交类似
				data:{
					"startTime":startTime,
					"endTime":endTime
				},
 				success:function(response){
					if (response['data'].length > 0){	
				        var tableBodys = '';
						for(var i=0;i<response['data'].length;i++){
            				var delHtml = '<a href="javascript:" onclick="deleteAnsiblePlaybookLogs(this,'+ response['data'][i].id + ')">' +	
				            				'<button class="btn btn-danger btn-xs">' +						                                					
												'<i class="glyphicon glyphicon-trash  bigger-110 icon-only"></i>' +	
											'</button>' +	
										  '</a>'							
	                    	var tableBody = '<tr class="odd gradeX" id="modelLogs-' + response['data'][i].id + '">' +
				                			'<td class="text-center">' + response['data'][i].id + '</td>' + 
				                			'<td class="text-center">' + response['data'][i].ans_id + '</td>' +
				                			'<td class="text-center">' + 
                            					'<a href="/apps/playbook/modf/'+ response['data'][i].ans_id + '/">'	+
                            						response['data'][i].ans_name +
				                				'</a>' +
				                			'</td>' +
				                			'<td class="text-center">' + response['data'][i].ans_content + '</td>' +
/* 				                			'<td class="text-center">' + response['data'][i].ans_server + '</td>' + */
				                			'<td class="text-center">' + response['data'][i].create_time.replace("T",' ').replace(/-/g,'/').slice(0,16) + '</td>' +				                                			
				                    		'<td class="text-center">' +
												'<a href="javascript:" onclick="getAnsibleResult(this,\'playbook\','+ response['data'][i].id + ')">' + 
				                            		'<button class="btn btn-primary btn-xs"  data-toggle="modal" data-target="#myAnsibleModelModal">' +					                                					
														'<i class="glyphicon glyphicon-zoom-in  bigger-110 icon-only"></i>' +
													'</button>' +
												'</a>';	
												if (response['perm'] == 1){
													var tableBody = tableBody + delHtml
												}
												else{
													var tableBody =  tableBody + '</td></tr>';
												}
	
							var tableBodys = tableBody + tableBodys;
						};	
					    var  tableHmtl = '<table width="100%" class="table table-striped table-bordered table-hover" id="playbookTableList">' +
				        '<thead>' + 
				            '<tr>' +
				            	'<th class="text-center">任务id</th>' + 
				            	'<th class="text-center">操作用户</th>' +
				                '<th class="text-center">剧本名称</th>' +
				                '<th class="text-center">剧本内容</th>' +
/* 				                '<th class="text-center">服务器</th>' + */
				                '<th class="text-center">操作时间</th>' +
				                '<th class="text-center">操作</th>' +				                                        
				            '</tr>' +				            
				        '</thead>' +
				        '<tbody>' + tableBodys + '</tbody></table>';
					    document.getElementById("playbookTableList").innerHTML = tableHmtl;
					}
					else{
						window.wxc.xcConfirm("未查询到相关数据", window.wxc.xcConfirm.typeEnum.error);
					}
				}	 			
			})					
		}		
		
		
		$(document).ready(function() {
		    $('#playbookTableList').DataTable({
		        responsive: true,
		        "order": [[5, 'asc']]
		    });
		});

		$(document).ready(function() {
		    $('#modelTableList').DataTable({
		        responsive: true
		    });
		});
				
		function deleteAnsibleModelLogs(obj,id) {
			$.ajax({
				dataType: "JSON",
				url:'/api/logs/ansible/model/'+ id +'/', //请求地址
				type:"DELETE",  //提交类似
				success:function(response){
					var str= 'modelLogs-' + id
					document.getElementById(str).innerHTML = ''; 
				}					
			})					
		}	

		function deleteAnsiblePlaybookLogs(obj,id) {
			$.ajax({
				dataType: "JSON",
				url:'/api/logs/ansible/playbook/'+ id +'/', //请求地址
				type:"DELETE",  //提交类似
				success:function(response){
					var str= 'playbookLogs-' + id
					document.getElementById(str).innerHTML = ''; 
				}					
			})					
		}		
		
		function getAnsibleResult(obj,model,id) {
			$.ajax({
				dataType: "JSON",
				url:'/apps/log/'+ model + '/'+ id +'/', //请求地址
				type:"POST",  //提交类似
				success:function(response){
					$("#ansible_task_result").html(response["data"]); 
				}					
			})					
		}		
		
</script>

{% endblock %}